﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace EntityMap.Meta
{
    /// <summary>
    /// This class manage all metadata of entity.
    /// </summary>
    [XmlRoot("entityInfo")]
    public class EntityInfo
    {
        [XmlArray( "aliases" )]
        [XmlArrayItem( typeof( Alias ), ElementName = "alias" )]
        public Collection<Alias> aliases = new Collection<Alias>();

        [XmlArray("sqlStatements")]
        [XmlArrayItem( typeof( SelectStatement ), ElementName = "selectStatement" )]
        [XmlArrayItem( typeof( SingleSelectStatement ), ElementName="singleSelectStatement" )]
        [XmlArrayItem( typeof( ChangeStatement ), ElementName = "changeStatement" )]
        [XmlArrayItem( typeof( StoredProcedure ), ElementName = "storedProcedure" )]
        public Collection<SqlStatement> Statements = new Collection<SqlStatement>();


        [XmlArray( "maps" )]
        [XmlArrayItem( typeof( ResultMap ), ElementName = "resultMap" )]
        [XmlArrayItem( typeof( ParameterMap ), ElementName = "parameterMap" )]
        public Collection<Map> Maps = new Collection<Map>();

        public EntityInfo() {}

        public SqlStatement GetStatement( string statementId )
        {
            foreach( SqlStatement statement in Statements )
            {
                if( statement.Id == statementId )
                {
                    return statement;
                }
            }
            throw new EntityMapException( String.Format( "The statement {0} not exist", statementId ) );
        }

        public ResultMap GetResultMap( string mapId )
        {
            for( int i = 0; i < Maps.Count; i++ )
            {
                if( Maps[i].Id == mapId )
                {
                    return Maps[i] as ResultMap;
                }
            }
            throw new EntityMapException( String.Format( "The map with id {0} not exist", mapId ) );
        }

        public ParameterMap GetParameterMap( string mapId )
        {
            for( int i = 0; i < Maps.Count; i++ )
            {
                if( Maps[i].Id == mapId )
                {
                    return Maps[i] as ParameterMap;
                }
            }
            return null;
        }

        public void ApplyAlias()
        {
            foreach( Alias alias in aliases )
            {
                string newString = alias.Text.Trim();

                foreach( SqlStatement statement in Statements )
                {
                    if( !String.IsNullOrEmpty( statement.Command )  )
                    {
                        string ss = statement.Command.Trim();

                        statement.Command = ss.Replace( "{" + alias.Id + "}", newString );
                    }
                }
            }
        }
    }
}
